package com.qianfeng.gl4study.youshare.utils;

import com.qianfeng.gl4study.youshare.model.Product;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * I'm glad to share my knowledge with you all.
 * User:Gaolei
 * Date:2015/2/3
 * Email:pdsfgl@live.com
 */
public class UtilityClass {


    /**
     * 自己设置连接方式来获取网络的文本数据，需要开放网络权限
     * @param path   URL
     * @param method get/post
     */
    public static byte[] getDataFromIntent(String path, String method) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpParams httpParams = httpClient.getParams();
        // 请求延时时间
        HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
        // 读取内容时间
        HttpConnectionParams.setSoTimeout(httpParams, 3000);
        HttpResponse response = null;
        try {
            if (method.equalsIgnoreCase("get")) {
                HttpGet httpGet = new HttpGet(path);
                response = httpClient.execute(httpGet);
            } else {
                HttpPost httpPost = new HttpPost(path);
                response = httpClient.execute(httpPost);
            }
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();
                byte[] byteArray = EntityUtils.toByteArray(entity);
                return byteArray;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null) {
                httpClient.getConnectionManager().shutdown();
            }
        }
        return null;    //失败返回null
    }


    /**
    将网络上获取的数据转化成Goods对象
    参数	byte[] bytes     从网络获取的字节数据
    返回值 List<Goods>  转化后的商品信息
    */
    public static List<Product> getListFromString(byte[] bytes){
        List<Product> listGoods = new LinkedList<Product>();
        try {

            String data = new String(bytes,"utf-8");
            JSONObject jsonGoods = new JSONObject(data);

            JSONArray msg = jsonGoods.getJSONArray("Goods");
            for(int i =0;i<msg.length();i++){
                JSONObject jb = msg.getJSONObject(i);
                Map<String ,Product> map = new HashMap<String, Product>();
                Product object = new Product();

                /*
                //添加商品信息获取的实现
                object.setTitle(jb.getString("title"));
                object.setContent(jb.getString("content"));
                object.setType(jb.getString("choiceType"));
                object.setRightAnswer(jb.getString("rightAnswer"));
                object.setCreateTime(jb.getString("createTime"));
                object.setState(0);
                object.setObjectLevel(jb.getString("ObjectLevel"));
                object.setId(jb.getInt("id"));
                */

                listGoods.add(object);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();    //转化字符串失败
        }
        return listGoods;
    }



}
